#include "PlayState.h"

PlayState::PlayState(GameContext& gc)
	: GameState(gc)
{
}

PlayState::~PlayState()
{
}

void PlayState::Load()
{
	mDude.Load();
	mDude.SetPosition(SVector2(512.0f, 386.0f));

	char levelName[256];
	char texturePackName[256];
	sprintf_s(levelName, 256, "level0%d.txt", mGameContext.GetLevel());
	sprintf_s(texturePackName, 256, "texturepack0%d.txt", mGameContext.GetLevel());
	mMap.Load(levelName, texturePackName);
}

void PlayState::Unload()
{
	mDude.Unload();
	mMap.Unload();
}

NextState PlayState::Update(float deltaTime)
{
	mDude.Update(deltaTime, mMap);

	NextState nextState = None;
	if (Input_IsKeyPressed(Keys::ESCAPE))
	{
		nextState = Frontend;
	}
	return nextState;
}

void PlayState::Render()
{
	const int kWinWidth = IniFile_GetInt("WinWidth", 800);
	const int kWinHeight = IniFile_GetInt("WinHeight", 600);

	SVector2 viewTarget = mDude.GetPosition();

	SVector2 viewOffset;
	viewOffset.x = viewTarget.x - (kWinWidth * 0.5f);
	viewOffset.y = viewTarget.y - (kWinHeight * 0.5f);

	SVector2 limit;
	limit.x = (float)mMap.GetWidth() - kWinWidth;
	limit.y = (float)mMap.GetHeight() - kWinHeight;
	
	viewOffset.x = Clamp(viewOffset.x, 0.0f, limit.x);
	viewOffset.y = Clamp(viewOffset.y, 0.0f, limit.y);

	mMap.Render(viewOffset);
	mDude.Render(viewOffset);
}